//
//  caigouxqViewController.m
//  BAOASystem
//
//  Created by michael on 2018/3/6.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "OALoginViewController.h"
#import "fajianxqViewController.h"
#import "demandTableViewCell.h"
#import "demandfirstTableViewCell.h"
#import "caigouxqViewController.h"
#import "qiyexqViewController.h"
#import "comnewsTableViewCell.h"
#import "shangpinxqViewController.h"
#import "pdnewsTableViewCell.h"
#import "firstproductxqTableViewCell.h"
@interface caigouxqViewController ()

@end

@implementation caigouxqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self requestdata];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];

//self.extendedLayoutIncludesOpaqueBars = YES;
//
//if (@available(iOS 11.0, *)) {
//
//UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//
//} else {
//
//    self.automaticallyAdjustsScrollViewInsets = NO;
//
//}


}
- (void)backaction{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

//    self.navigationController.navigationBarHidden = YES;
////    [self.navigationController setNavigationBarHidden:YES animated:animated];
//self.automaticallyAdjustsScrollViewInsets = NO;

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    self.navigationController.navigationBarHidden = NO;

}

- (void)requestdata{
    NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
  dics[@"dmdid"] = _pdid;

  [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *dicstring = [httpTool dictionaryToJson:dics];
//dicstring= [dicstring URLDecodedString];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/demand/yellowpage?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
 NSLog(@"%@",dictionary);

  [MBProgressHUD hideHUDForView:self.view animated:YES];
  _dic = dictionary;
//  _imagearray = dictionary[@"pdimage"];
  _productdic = dictionary[@"demand"];
  _companydic = dictionary[@"dmdcompany"];
[self initview];
 }andFailure:^{
  [MBProgressHUD hideHUDForView:self.view animated:YES];

//         [self.tableView headerEndRefreshing];

 }];

}
- (void)initview{
if(!_tableView){
 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.bounces = NO;
_tableView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
[self.view addSubview:_tableView];
 [_tableView registerNib:[UINib nibWithNibName:@"comnewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"comnewsTableViewCell"];

//UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//backbutton.frame = CGRectMake(10, 20, 40, 40);
//[backbutton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
//[backbutton addTarget:self action:@selector(backaction) forControlEvents:UIControlEventTouchUpInside];
//[self.tableView addSubview:backbutton];

}

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//return 5;
if (section==0) {
    return 3;
}else if (section==1){
return 1;
}else{

return 0;
}

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
    

if (indexPath.row ==0) {
         static NSString *CellIdentifier0 = @"Cellbendititlepic0";

demandfirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (!cell) {
    cell = [[demandfirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
                 cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
cell.dict = _productdic;
//cell.array = _imagearray;
return cell;
}else if (indexPath.row ==1){
 comnewsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"comnewsTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell = [[[UINib nibWithNibName:@"comnewsTableViewCell" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    cell.dict =_companydic;
    cell.type = @"caigou";
    return cell;


}else {
 static NSString *CellIdentifier2 = @"Cellbendititlepic2";

    demandTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
if (cell==nil) {
        cell=[[demandTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
cell.dic = _dic;
    return cell;

}
} else {
 static NSString *CellIdentifier4 = @"Cellbendititlepic4";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
//    cell.backgroundColor = [UIColor redColor];

if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];


    }
             UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
      button1.frame = CGRectMake(10, 20, (KScreenWidth-20), 40);
          button1.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

  [button1 setTitle:NSLocalizedString(@"商品详情-发送联系信", nil) forState:UIControlStateNormal];
    button1.titleLabel.font = [UIFont systemFontOfSize: 14.0];
   [button1 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
 [button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];

            [cell.contentView addSubview:button1];

    return cell;

}



}
- (void)xqaction:(UIButton *)button {
    self.isExpand=!self.isExpand;

//if (button.selected) {
//    _type = @"stay";
//    button.selected = NO;
//}else{
//    _type = @"nostay";
//    button.selected = YES;

//}
//button.selected = !button.selected;

[_tableView reloadData];
}
- (void)action1{
   UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]&&![[DanLi sharedDanLi].stay isEqualToString:@"yes"]) {
      OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationController.navigationBar.hidden = YES;
                 self.view.window.rootViewController = navVC;

}else{
fajianxqViewController *vc = [[fajianxqViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;

//@property (strong, nonatomic)  NSString *toMemberCompanyId;
//@property (strong, nonatomic)  NSString *toMemberNameId;
//
//@property (strong, nonatomic)  NSString *productId;

        NSDictionary * dic = _dic;
//vc.productId = dic[@"id"];
vc.toMemberNameId = _productdic[@"userId"];

vc.toMemberCompanyId = _productdic[@"cpid"];
[self.navigationController pushViewController:vc animated:YES];

}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {

if (indexPath.row ==0) {
    return +70+10+20;
}else if(indexPath.row ==1){
//return 140+5+5+20+30+10+60+12+30;
NSDictionary *dic =_companydic;
     CGFloat height = [OAToolBox sizebyText:dic[@"address"] font:13 minsWidth:100];
          CGFloat height2 = [OAToolBox sizebyText:dic[@"websites"] font:13 minsWidth:100];
//    return 140+5+5+20+30+10+60+12+15+15;

     if ((height>31.5)&&(height2>31.5)) {
    return 140+5+5+20+30+10+60+12+15+15;
}else if(height>31.5){
   return 140+5+5+20+30+10+60+12+15;

}else if(height2>31.5){
    return 140+5+5+20+30+10+60+12+15;

}else{
return 140+5+5+20+30+10+60+12;
}
}else{
NSArray *pricearray  =_dic[@"dmdprice"];
NSArray *attributearray  =_dic[@"dmdattribute"];
NSDictionary *dic  =_dic[@"product"];
     CGFloat height = [OAToolBox sizebyText:dic[@"keywords"] font:13 minsWidth:120];
NSDictionary *dic2 = _dic[@"demand"];
     CGFloat height2 = [OAToolBox sizebyText:dic2[@"newRemark"] font:13 minsWidth:80];
     if (height2<32) {
    height2 = 32;
}
if (pricearray.count==0) {
//return height2+32+2*32+attributearray.count*32+30;
  return height2+32+2*32+attributearray.count*32+30+0;


}else{
return height2+32+1*32+attributearray.count*32+(pricearray.count+1)*32+30+0;

NSLog(@"11111111");
}
}
}


else {

return 150;
}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

